#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/rope>
#define rep(i,l,r) for(int i=(l),i##end=(r);i<=i##end;++i)
#define per(i,r,l) for(int i=(r),i##end=(l);i>=i##end;--i)
#define ll long long
#define int ll
#define double long double
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define popcnt __builtin_popcount
#define rbtree(way) tree<way,null_type,less<way>,rb_tree_tag,tree_order_statistics_node_update>
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
const int maxn=1e6+10,maxm=1e6+10,mod=998244353,inf=INT_MAX;
inline int ksm(int x,int k,int mod=mod){
	int ans=1;
	for(x%=mod;k;k>>=1,x=x*x%mod) if(k&1) ans=ans*x%mod;
	return ans;
}

int T,L,R,mi[maxn];

int LU(int dep,int x){
	if(!dep) return 1;
	int n=1<<dep-1;
	if(x<=n) return LU(dep-1,x);
	return (LU(dep-1,x-n)<<1)+mi[dep-1];
}
int RU(int dep,int x){
	if(!dep) return 1;
	int n=1<<dep-1;
	if(x<=n) return (RU(dep-1,x)<<1)+mi[dep-1];
	return RU(dep-1,x-n);
}

int solve(int dep,int L,int R){
	if(!dep) return 1;
	int n=1<<dep-1,x=R-L+1;
	if(L<=n&&R<=n) return solve(dep-1,L,R);
	if(L<=n&&x<=n) return LU(dep-1,R-n)+mi[dep-1]-(L<n?RU(dep-1,L+1):0);
	if(L<=n) return solve(dep-1,L,R-n)+mi[dep-1]-(L<n?RU(dep-1,L+1):0);
	return solve(dep-1,L-n,R-n)+mi[dep-1]+LU(dep-1,R-n);
}

signed submain(){
	int ans=0;
	cin>>L>>R,++L,++R,cout<<LU(30,R)-(L>1?solve(30,L-1,R):0)<<'\n';
	return 0;
}

signed main(){
	freopen("comb.in","r",stdin),freopen("comb.out","w",stdout);
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>T,mi[0]=1;
	rep(i,1,31) mi[i]=mi[i-1]*3;
	rep(o,1,T) submain();
	return 0;
}
